<template>
  <el-dialog :visible="show" title="分配权限" @close="closeEvent">
    <el-tree
      :data="treeList"
      :props="{ label: 'name' }"
      node-key="id"
      show-checkbox
      check-strictly
      ref="tree"
    ></el-tree>
    <template #footer>
      <div style="text-align:center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission.js'
import { sysRoleGet, sysRolePut } from '@/api/setting.js'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  created () {
    this.getData()
  },
  data () {
    return {
      treeList: [], // 树形权限点数据
      info: ''
    }
  },
  methods: {
    // 取消按钮
    closeEvent () {
      this.$emit('update:show', false)
    },
    // 获取所有权限点数据
    async getData () {
      const res = await sysPermission()
      this.treeList = this.changeData(res.data, '0')
      console.log('所有权限点', res)
      console.log('树形权限点', this.treeList)
    },
    // 将权限点数据转换成树形数据
    changeData (arr, str = '0') {
      return arr.filter(item => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 获取角色详情
    async getRoleInfo (id) {
      const res = await sysRoleGet(id)
      console.log('角色详情', res)
      this.info = res.data
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 确认点击
    async submit () {
      await sysRolePut({
        ...this.info,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('更新权限成功')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
